/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.utils;

import java.util.Vector;

public class VectorBindable {
    private Vector store = new Vector();
    
    public VectorBindable() {}
    
    public VectorBindable(Bindable bindable)
    throws BindException {
        for (;bindable != null;bindable = bindable.getNextBindable()) {
            add(bindable);
        }
    }
    
    public VectorBindable(Bindable bindable[]) {
        for (int i =0;i < bindable.length;i++) {
            add(bindable[i]);
        }
    }
    
    public VectorBindable(Vector vector) {
        for (int i = 0;vector != null && i < vector.size();i++) {
            add((Bindable) vector.get(i));
        }
    }
    
    public Bindable get(int pos) {
        return (Bindable)store.get(pos);
    }
    
    public Bindable [] toArray()
    throws BindException {
        Bindable items[] = new Bindable[size()];
        for (int i = 0;i < size();i++) {
            items[i] = get(i);
        }
        return items;
    }
    
    public boolean contains(Bindable item){
        return store.contains(item);
    }
    
    public void add(Bindable item) {
        store.add(item);
    }
    
    public void add(int index,Bindable item) {
        try {
            store.add(index,item);
        } catch (Exception exception) {
            exception.printStackTrace();
            System.exit(1);
        }
    }
    
    public void set(int index,Bindable item) {
        store.set(index,item);
    }
    
    public boolean remove(Bindable item) {
        return store.remove(item);
    }
    
    public Bindable remove(int pos) {
        return (Bindable) store.remove(pos);
    }
    
    public int size() {
        return store.size();
    }
    
    public boolean shouldSave() {
        for (int i =0;i < size();i++){
            if (get(i).shouldSave()) {
                return true;
            }
        }
        return false;
    }
    
    public void save()
    throws BindException {
        for (int t = 0;t < size();t++) {
            get(t).save();
        }
    }
    
    public void commit()
    throws BindException {
        for (int t = 0;t < size();t++) {
            get(t).commit();
        }
    }
    
    public void removeAll(){
        store.removeAllElements();
    }
    
    public void deleteAll()
    throws BindException {
        for (int t = 0;t < size();t++) {
            get(t).delete();
        }
    }
    
    public int indexOf(Bindable item) {
        return store.indexOf(item);
    }
    
    public void reload()
    throws BindException {
        for (int t = 0;t < size();t++) {
            if (get(t).isLoaded()){
                get(t).reload();
            }
        }
    }
    
    public Vector getVector() {
        return store;
    }
}

